package cn.attackme.myuploader.controller;

import cn.attackme.myuploader.model.ExcelContent;
import cn.attackme.myuploader.model.Img;
import cn.attackme.myuploader.result.ResponseStatues;
import cn.attackme.myuploader.result.Result;
import cn.attackme.myuploader.result.ResultJson;
import cn.attackme.myuploader.service.FileService;
import cn.attackme.myuploader.utils.OCR;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 文件上传
 */
@RestController
@RequestMapping("/File")
@CrossOrigin
public class FileUploadController {
    @Autowired
    private FileService fileService;
    @Value("${server.httpurl}")
    private String httpurl;

    private ResultJson resultJson = new ResultJson();

    /**
     * 增值税普通发票
     * @param name
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/file1")
    public JSONObject upload(String name,
                             MultipartFile file) throws IOException {
        String fileSplit[] = name.split("\\.");
        String fileType = fileSplit[fileSplit.length-1];
        Img img = null;
        if(fileType.equals("pdf")||fileType.equals("PDF")){
            ExcelContent excelContent = fileService.uploadPDF(file);
            if(excelContent==null) {
                resultJson = Result.fail(ResponseStatues.UPLOAD_ERROR);
            }else{
                if(excelContent.getCheck().length()>=6) excelContent.setCheck(excelContent.getCheck().substring(excelContent.getCheck().length()-6));
                excelContent.setFilename(name);
                resultJson = Result.success(excelContent);
            }

        }else if(fileType.equals("zip")){
            System.out.println("zip");
            List<ExcelContent> excelContentList = fileService.uploadZIP(httpurl,file);
            resultJson = Result.success(excelContentList);
        }else{
            //图片格式
            img = fileService.uploadImg(name,file);
            if(img==null){
                resultJson = Result.fail(ResponseStatues.UPLOAD_ERROR);
            }else{
                //OCR识别
                ExcelContent excelContent = OCR.getResult(httpurl,JSON.toJSONString(img));
                if(excelContent==null) {
                    resultJson = Result.fail(ResponseStatues.UPLOAD_ERROR);
                }else{
                    excelContent.setCheck(excelContent.getCheck().substring(excelContent.getCheck().length()-6));
                    excelContent.setFilename(img.getFilename());
                    resultJson = Result.success(excelContent);
                }
            }
        }

        return JSON.parseObject(JSON.toJSONString(resultJson));
    }

    /**
     * 非税收入票据
     * @param name
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/file2")
    public JSONObject upload2(String name,
                             MultipartFile file) throws IOException {
        String fileSplit[] = name.split("\\.");
        String fileType = fileSplit[fileSplit.length - 1];
        if (fileType.equals("pdf")||fileType.equals("PDF")) {
            ExcelContent excelContent = fileService.uploadPDF2(file);
            if (excelContent == null) {
                resultJson = Result.fail(ResponseStatues.UPLOAD_ERROR);
            } else {
                resultJson = Result.success(excelContent);
            }

        }
        return JSON.parseObject(JSON.toJSONString(resultJson));
    }
    @RequestMapping(value = "/download", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public JSONObject download(@RequestBody List<ExcelContent> excelContents){
        String xlsurl = fileService.SaveToExcel(excelContents);
        if(xlsurl.isEmpty()){
            resultJson = Result.fail(ResponseStatues.DOWNLOAD_ERROR);
        }else{
            resultJson = Result.success(xlsurl);
        }
        return JSON.parseObject(JSON.toJSONString(resultJson));
    }

    public void upload(String name,
                       String md5,
                       MultipartFile file) throws IOException {
        fileService.upload(name, md5,file);
    }
}
